package com.tomvian.twodee.common 
{
	import flash.display.MovieClip;
	/**
	 * CachingStateDisplay
	 * @author Thomas Vian
	 */
	public class CachingStateDisplay extends StateDisplay
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		protected var _cachedStates:Array;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		override public function set state(value:String):void
		{
			if (_state != value)
			{
				_state = value;
				
				if (_cachedStates[_state])
				{
					_target.cacheAsBitmap = true;
				}
				else
				{
					_target.cacheAsBitmap = false;
				}
				
				switch(_pathDepth)
				{
					case 0:
					{
						_target.gotoAndStop(_state);
						break;
					}
					case 1:
					{
						MovieClip(_target[_path0]).gotoAndStop(_state);
						break;
					}
					case 2:
					{
						MovieClip(_target[_path0][_path1]).gotoAndStop(_state);
						break;
					}
					case 3:
					{
						MovieClip(_target[_path0][_path1][_path2]).gotoAndStop(_state);
						break;
					}
					case 4:
					{
						MovieClip(_target[_path0][_path1][_path2][_path3]).gotoAndStop(_state);
						break;
					}
				}
			}
		}
		
		override public function set stateNum(value:uint):void
		{
			if (_stateNum != value)
			{
				_stateNum = value;
				
				if (_cachedStates[_stateNum])
				{
					_target.cacheAsBitmap = true;
				}
				else
				{
					_target.cacheAsBitmap = false;
				}
				
				switch(_pathDepth)
				{
					case 0:
					{
						_target.gotoAndStop(_stateNum);
						break;
					}
					case 1:
					{
						MovieClip(_target[_path0]).gotoAndStop(_stateNum);
						break;
					}
					case 2:
					{
						MovieClip(_target[_path0][_path1]).gotoAndStop(_stateNum);
						break;
					}
					case 3:
					{
						MovieClip(_target[_path0][_path1][_path2]).gotoAndStop(_stateNum);
						break;
					}
					case 4:
					{
						MovieClip(_target[_path0][_path1][_path2][_path3]).gotoAndStop(_stateNum);
						break;
					}
				}
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function CachingStateDisplay(target:MovieClip, cachedStates:Array, path:String = "") 
		{
			super(target, path);
			
			_cachedStates = [];
			for(var i:uint = 0, l:uint = cachedStates.length; i < l; i++)
			{
				_cachedStates[cachedStates[i]] = true;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
	}
}